<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//统计日报
	//1 本部和分部的总量
	//2  按照日期查询
	//3 同期对比（去年的今天）
	//4 科室 排名和ghCount
	//5 专家 普通 ，急诊等类别进行分类汇总数量
	String date = input.getDesc("date","2017-11-21");//开始时间
	String endDa = input.getDesc("date","2017-11-21");//结束时间
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	
	//存放返回结合
	Map result = new HashMap();
	/******************2 本部和分部的总量(某一时间点)*********************/
	//本部总数量查询
	List<Map> listCount1 = QBuilder.db("basedb").queryHql("select new map(count(*) as count) from mzjl where ksdm in (select new map(id as id) from his_dept where yydm ='01') and  ghrq >'"+startDate+"' and ghrq <'"+endDate+"'");
	//广瑞路总数量查询
	List<Map> listCount2 = QBuilder.db("basedb").queryHql("select new map(count(*) as count) from mzjl where ksdm in (select new map(id as id) from his_dept where yydm ='02') and ghrq >'"+startDate+"' and ghrq <'"+endDate+"'");
	System.out.println(listCount1.get(0));
	Integer count = (listCount1 == null || listCount1.size() == 0) ? 0 : Tools.getInt(listCount1.get(0).get("count").toString(),0);
	Integer count2 = (listCount2 == null || listCount2.size() == 0) ? 0 : Tools.getInt(listCount2.get(0).get("count").toString(),0);
	result.put("count",count);
	result.put("count2",count2);
	
	/******************3 同期对比（去年的今天）疑问 1,2,3的区别*********************/
	
	
	/******************4 科室 排名和ghCount*********************/
	//select new map(y.id as id,y.name as name ,case  when ghCount IS NULL  then 0 ELSE  g.ghCount end )  from his_dept y LEFT JOIN (select new map(ksdm as ksdm,ksmc as ksmc, count(ksdm) as ghCount ) from  mzjl   where ghrq >'2017112100:00:00' and ghrq <'2017112123:59:59' GROUP BY ksdm ) g  on g.ksdm = y.id where y.yydm =02  ORDER BY g.ghCount desc
	String sqlQue = "select y.id,y.name ,case  when 挂号数量 IS NULL  then 0 ELSE  g.[挂号数量] END 挂号数量  from YY_KSBMK y LEFT JOIN (select g.ksdm,g.ksmc , count(*) as 挂号数量  from  GH_GHZDK g  where ghrq >'2017112100:00:00' and ghrq <'2017112123:59:59' GROUP BY ksdm ,ksmc ) g  on g.ksdm = y.id where y.yydm =02  ORDER BY g.挂号数量 desc ";
	String lbsqlQue = "select gh.lbmc ,case  when 挂号数量 IS NULL  then 0 ELSE  g.[挂号数量] END 挂号数量 from GH_GHLBK gh LEFT JOIN (select  count(*) as 挂号数量 ,ghlb from  GH_GHZDK g  where ghrq >'2017112100:00:00' and ghrq <'2017112123:59:59' GROUP BY ghlb) g  on g.ghlb = gh.lbxh ";
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
       
	try {
		 ss.beginTransaction();
		 SQLQuery sqlQuery = ss.createSQLQuery(sqlQue);
		 sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         List<Map> list = sqlQuery.list();
		 
		 /******************5 专家 普通 ，急诊等类别进行分类汇总数量*********************/
		 SQLQuery lbsqlQuery = ss.createSQLQuery(lbsqlQue);
		 lbsqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         List<Map> lblist = lbsqlQuery.list();
		 
		 ss.getTransaction().commit();
		 ss.close();
		result.put("deptList",list);
		result.put("lblist",lblist);
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
    }
	
	Message msg = new Message();
	msg.put("errcode",0);
	msg.put("data",result);
	out.println(msg);
%>